﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMSModel;

public partial class new_add : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bd();
        }
    }

    private void bd()
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        string newsTypeId = "";
        if (Request["newsTypeId"] != null && Request["newsTypeId"] != "null" && !string.IsNullOrEmpty(Request["newsTypeId"]))
        {
            newsTypeId = Request["newsTypeId"].ToString();
            lblSelectType.Text = dal.NewsType.First(x => x.id == newsTypeId).name;
        }
        DataList1.DataSource = dal.NewsType.Where(x => string.IsNullOrEmpty(x.pid));
        DataList1.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string newsTypeId = "";
        try
        {
            if (Request["newsTypeId"] != null && !string.IsNullOrEmpty(Request["newsTypeId"]))
            {
                newsTypeId = Request["newsTypeId"].ToString();
            }
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            CMSModel.News news = new CMSModel.News();
            //上传图片
            news.ImageUrl = Image1.ImageUrl;
            news.author = txtAuthor.Text;
            news.ctime = DateTime.Now;
            news.content = NewsImportBLL.ReplaceGP(txtContent.Text);
            news.keyword = txtKeyWords.Text;
            news.title = txtTitle.Text;
            news.Summary = txtSummary.Text;
            news.Stock = txtStock.Text;
            news.Nature = int.Parse(ddlNature.SelectedValue);
            news.Source = txtSource.Text;
            news.step = int.Parse(ddlStep.SelectedValue);
            if (!string.IsNullOrEmpty(txtPTime.Text))
            {
                news.ptime = DateTime.Parse(txtPTime.Text);
            }
            news.OutsideUrl = txtOutsideUrl.Text;
            if (Request["aa"] == "1")
            {
                news.IsEnable = true;
                news.ptime = news.ctime;
            }
            else
            {
                news.IsEnable = false;
            }

            //添加相关股票
            if (!string.IsNullOrEmpty(txtStock.Text))
            {
                string[] cc = txtStock.Text.Split(',', '，');
                foreach (var c in cc)
                {
                    NewsRelatedStocks s = new NewsRelatedStocks();
                    s.NewsId = news.id;
                    s.Stock = c;
                    try
                    {
                        s.StockName = CommonBll.DicStocks[s.Stock];
                    }
                    catch
                    {
                    }
                    dal.NewsRelatedStocks.AddObject(s);
                }
            }

            string str = hidSelectValue.Value;
            string str1 = lblSelectType.Text;
            if (!string.IsNullOrEmpty(str) || !string.IsNullOrEmpty(str1))
            {
                if (!string.IsNullOrEmpty(str))
                {
                    foreach (var c in str.Split(','))
                    {
                        news.News_Type.Add(new CMSModel.News_Type() { ctime = DateTime.Now, News = news, NewsTypeId = c });
                    }
                }
                if (!string.IsNullOrEmpty(str1) && string.IsNullOrEmpty(str))
                {
                    news.News_Type.Add(new CMSModel.News_Type() { ctime = DateTime.Now, News = news, NewsTypeId = newsTypeId });
                }
                dal.News.AddObject(news);
                dal.SaveChanges();
                CacheClearBLL.NewsClear(news.id);
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');document.location='default.aspx?newsTypeId=" + Request["newsTypeId"] + "&page=" + Request["page"] + "'", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('未选择新闻类型！');", true);
            }
        }
        catch (Exception ex)
        {
            LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "AddNews", LogYuMin.LogStates.ERROR, ex);
            ClientScript.RegisterClientScriptBlock(GetType(), "aa", "alert('添加失败,请重试');", true);
            string str = hidSelectValue.Value;
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            string lbl = "";
            foreach (var c in str.Split(','))
            {
                lbl += dal.NewsType.Where(x => x.id == c).First().name + ",";
            }
            lbl = lbl.Substring(0, lbl.Length - 1);
            lblSelectType.Text = lbl;
        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        var c = (NewsType_NewsTypeSelect)e.Item.FindControl("NewsTypeSelect1");
        c.BindData(0);
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?newsTypeId=" + Request["newsTypeId"] + "&page=" + Request["page"]);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string imageUrl = null;
        if (FileUpload1.HasFile)
        {
            var data = FileUpload1.FileBytes;
            var fileName = FileUpload1.FileName;
            FileBLL fileBLL = new FileBLL();
            imageUrl = fileBLL.UpNewsImage(data, fileName);
            Image1.Visible = true;
            Image1.ImageUrl = imageUrl;
        }
    }
}